﻿@using SIGBugs.Shared.Models
@using Microsoft.AspNetCore.Blazor.Browser.Interop;
@using SIGBugs.Client.Libs;

@page "/addproject"
@inject HttpClient Http
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>创建项目</h1>

<hr />
<div class="row">
    <div class="col-md-4">
        <form>
            @if (!string.IsNullOrEmpty(errorMessage))
            {
                <div class="alert alert-danger">
                    <span class="oi oi-circle-x"></span>  @errorMessage
                </div>

            }

                <div class="form-group @(isInvalidName?"was-validated":"")">
                    <label for="Name" class="control-label">项目名称</label>
                    <input for="Name" ref="inputName" class="form-control" bind="@project.Name" required />
                    <div class="invalid-feedback">
                        请输入项目名称
                    </div>
                </div>
            <div class="form-group">
                <label asp-for="Gender" class="control-label">描述</label>
                <textarea asp-for="Description" bind="project.Description" class="form-control"></textarea>

            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary" onclick="@(async () => await CreateProject())">保存</button>
                <button class="btn" onclick="@cancel">取消</button>
            </div>
        </form>
    </div>
</div>


@functions {
    string errorMessage = string.Empty;
    bool isInvalidName = false;

    ElementRef inputName;

    Project project = new Project();
    protected override void OnInit()
    {
        RegisteredFunction.Invoke<bool>("log", "测试");
        base.OnInit();
    }
    protected override void OnAfterRender()
    {
        // RegisteredFunction.Invoke<bool>("tinymce", "textarea");
        inputName.Focus();
        base.OnAfterRender();
    }

    protected async System.Threading.Tasks.Task CreateProject()
    {
        if (string.IsNullOrEmpty(project.Name))
        {
          
            isInvalidName = true;
            inputName.Focus();          
            return;

        }

        project.CreatedBy = "admin";
        project.CreatedDate = DateTime.Now;

        await Http.SendJsonAsync(HttpMethod.Post, "/api/Projects", project);
        UriHelper.NavigateTo("/fetchproject");
    }
    void cancel()
    {
        UriHelper.NavigateTo("/fetchproject");
    }
}  